
package example

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
)

type WolfKillGameService struct {}
// CreateWolfKillGame 创建狼人杀对局记录
// Author [yourname](https://github.com/yourname)
func (WKGService *WolfKillGameService) CreateWolfKillGame(WKG *example.WolfKillGame) (err error) {
	err = global.GVA_DB.Create(WKG).Error
	return err
}

// DeleteWolfKillGame 删除狼人杀对局记录
// Author [yourname](https://github.com/yourname)
func (WKGService *WolfKillGameService)DeleteWolfKillGame(ID string) (err error) {
	err = global.GVA_DB.Delete(&example.WolfKillGame{},"id = ?",ID).Error
	return err
}

// DeleteWolfKillGameByIds 批量删除狼人杀对局记录
// Author [yourname](https://github.com/yourname)
func (WKGService *WolfKillGameService)DeleteWolfKillGameByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]example.WolfKillGame{},"id in ?",IDs).Error
	return err
}

// UpdateWolfKillGame 更新狼人杀对局记录
// Author [yourname](https://github.com/yourname)
func (WKGService *WolfKillGameService)UpdateWolfKillGame(WKG example.WolfKillGame) (err error) {
	err = global.GVA_DB.Model(&example.WolfKillGame{}).Where("id = ?",WKG.ID).Updates(&WKG).Error
	return err
}

// GetWolfKillGame 根据ID获取狼人杀对局记录
// Author [yourname](https://github.com/yourname)
func (WKGService *WolfKillGameService)GetWolfKillGame(ID string) (WKG example.WolfKillGame, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&WKG).Error
	return
}
// GetWolfKillGameInfoList 分页获取狼人杀对局记录
// Author [yourname](https://github.com/yourname)
func (WKGService *WolfKillGameService)GetWolfKillGameInfoList(info exampleReq.WolfKillGameSearch) (list []example.WolfKillGame, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.WolfKillGame{})
    var WKGs []example.WolfKillGame
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["start_at"] = true
         	orderMap["end_at"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&WKGs).Error
	return  WKGs, total, err
}
func (WKGService *WolfKillGameService)GetWolfKillGamePublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
